﻿using DataCollection.Core.Models;
using DataCollection.Infrastructure;
using DataCollection.WinForm.ViewModel;
using Masuit.Tools;
using Masuit.Tools.Strings;
using System.DirectoryServices.ActiveDirectory;
using System.Reflection.Metadata;

namespace DataCollection.WinForm.Forms
{
    public partial class AddProjectForm : DCBaseForm
    {
        #region 属性
        private ProjectViewModel viewModel = null!;

        /// <summary>
        /// 需要绑定的控件
        /// </summary>
        private Dictionary<string, Control> ControlDict = new Dictionary<string, Control>();

        public TemplateItem? TemplateService;

        #endregion

        #region 自定义方法
        /// <summary>
        /// 模板信息
        /// </summary>
        public void LoadTemplateInfo()
        {
            if (TemplateService is not null)
            {
                Dictionary<string, Object> where = new Dictionary<string, Object>();
                where["number"] = TemplateService.Number;
                List<TemplateModel> list = SqlSugarHelper.Inquireable<TemplateModel>(where);
                if (list.Count <= 0)
                {
                    Error("试压模板不存在，请重新选择");
                    return;
                }

                DesignPressureInput.Text = TemplateService.DesignPressure.ToString();

                if (TemplateService.PipePosition is not null)
                {
                    if (TemplateService.PipePosition.Contains("站内/集输"))
                    {
                        TestTypeSelect.Text = "站内/集输";
                    }
                    else if (TemplateService.PipePosition.Contains("站内"))
                    {
                        TestTypeSelect.Text = "站内";
                    }
                    else if (TemplateService.PipePosition.Contains("集输"))
                    {
                        TestTypeSelect.Text = "集输";
                    }
                }
            }
            DesignPressureInput.Enabled = true;
        }

        #endregion

        #region 系统事件
        public AddProjectForm()
        {
            InitializeComponent();
            Header.Text = "添加试压工程";
            ControlDict.Add("number", NumberInput);
            ControlDict.Add("name", NameInput);
            ControlDict.Add("stakeMark", StakeMarkInput);
            ControlDict.Add("pipeSpecification", PipeSpecificationInput);
            ControlDict.Add("pipeLength", PipeLengthInput);
            ControlDict.Add("medium", MediumInput);
            ControlDict.Add("designPressure", DesignPressureInput);
            ControlDict.Add("pipeNature", PipeNatureSelect);
            ControlDict.Add("testType", TestTypeSelect);
            ControlDict.Add("company", CompanyInput);
            ControlDict.Add("manometerPrecision", ManometerPrecisionSelect);
            ControlDict.Add("manometerRange", ManometerRangeSelect);
        }

        private void AddProjectForm_Load(object sender, EventArgs e)
        {
            viewModel = new ProjectViewModel(DateTimeOffset.Now.ToUnixTimeSeconds());
            foreach (var item in ControlDict)
            {
                Bindings(item.Value, "Text", item.Key, viewModel);
            }
        }
        #endregion

        private void ConfirmButton_Click(object sender, EventArgs e)
        {
            if (viewModel.Number.ToString().IsNullOrEmpty())
            {
                Error("请输入工程编号");
                return;
            }

            if (viewModel.Name.IsNullOrEmpty())
            {
                Error("请输入工程名称");
                return;
            }
            if (viewModel.StakeMark.IsNullOrEmpty())
            {
                Error("请输入工程桩号");
                return;
            }
            if (viewModel.PipeSpecification.IsNullOrEmpty())
            {
                Error("请输入管材规格");
                return;
            }
            if (viewModel.PipeLength.IsNullOrEmpty())
            {
                Error("请输入正确的管道长度");
                return;
            }
            if (viewModel.Medium.IsNullOrEmpty())
            {
                Error("请输入试压介质");
                return;
            }

            if (viewModel.DesignPressure.IsNullOrEmpty())
            {
                Error("请输入正确的设计压力");
                return;
            }

            if (viewModel.PipeNature.IsNullOrEmpty())
            {
                Error("请选择管道性质");
                return;
            }
            if (viewModel.TestType.IsNullOrEmpty())
            {
                Error("请选择试压类型");
                return;
            }
            if (viewModel.Company.IsNullOrEmpty())
            {
                Error("请输入施工单位");
                return;
            }
            if (viewModel.ManometerPrecision.IsNullOrEmpty())
            {
                Error("请选择压力表精度");
                return;
            }
            if (viewModel.ManometerRange.IsNullOrEmpty())
            {
                Error("请选择压力表量程");
                return;
            }
            if (TemplateService != null)
            {
                viewModel.TemplateID = TemplateService.Number;
            }
            if (viewModel.TemplateID.IsNullOrEmpty())
            {
                Error("请选择试压模板");
                return;
            }
            viewModel.Apply();
            Response response = viewModel.Add();
            if (response.Code == Response.SuccessCode)
            {
                if (this.Owner != null)
                {
                    ManageProjectForm frm = (ManageProjectForm)this.Owner;
                    frm.LoadList();
                    frm.Success(response.Msg);
                }
                this.Close();
            }
            else
            {
                Error(response.Msg);
            }
        }

        private void DesignPressureInput_TextChanged(object sender, EventArgs e)
        {

        }

        private void DesignPressureInput_Click(object sender, EventArgs e)
        {
            DesignPressureInput.Enabled = false;
            ManageTemplateForm frm = new ManageTemplateForm();
            frm.Type = 1;
            frm.Owner = this;
            frm.ShowDialog();
        }
    }
}
